package com.twu.biblioteca;

import java.util.LinkedList;

/******
 * Created by saylward on 6/03/2014.
 */
public class Library
{
    private InputOutput inputOutput;
    private LinkedList<Book> books;
    private LinkedList<Movie> movies;
    private LinkedList<User> users;
    private User loggedInUser;


    public Library(InputOutput inputOutput, LinkedList<Book> books, LinkedList<Movie> movies, LinkedList<User> users)
    {
        this.inputOutput = inputOutput;
        this.books = books;
        this.movies = movies;
        this.users = users;
        loggedInUser = null;
    }

    public Library()
    {
        inputOutput = new InputOutput();
        books = new LinkedList<Book>();
        movies = new LinkedList<Movie>();
        this.users = new LinkedList<User>();
        loggedInUser = null;
    }

    public void setupLibrary()
    {
        books.clear();
        books.add(new Book("A Book"));
        books.add(new Book("B Book"));
        books.add(new Book("C Book"));
        books.add(new Book("D Book"));
        books.add(new Book("E Book"));
        movies.clear();
        movies.add(new Movie("A Movie", "2014", "Mr Director", "10"));
        movies.add(new Movie("B Movie", "2013", "Mr Director", "9"));
        movies.add(new Movie("C Movie", "2012", "Mr Director", "8"));
        movies.add(new Movie("D Movie", "2011", "Mr Director", "7"));
        movies.add(new Movie("E Movie", "2010", "Mr Director", "6"));
        users.clear();
        users.add(new User("Frank", "123-4567", "password", "aoeu@aoeu.com", "12345678"));
        users.add(new User("Bob", "123-1234", "password", "aoeu@aoeu.com", "12345678"));
        users.add(new User("Bill", "111-1111", "password", "aoeu@aoeu.com", "12345678"));
    }

    public void loginUser()
    {
        inputOutput.loginUseLibraryNumber();
        String libraryNumber = inputOutput.getUserInput();
        inputOutput.loginUsePassword();
        String password = inputOutput.getUserInput();
        if (loginUser(libraryNumber, password))
            inputOutput.successfulLogin(loggedInUser.getName());
        else
            inputOutput.unsuccessfulLogin();
    }

    private boolean loginUser(String libraryNumber, String password)
    {
        for (User user : users)
            if (user.getLibraryNumber().equals(libraryNumber) && user.validatePassword(password))
            {
                loggedInUser = user;
                return true;
            }
        return false;
    }

    public void checkInBook()
    {
        if (loggedInUser == null)
        {
            inputOutput.youMustBeLoggedIn();
            return;
        }
        inputOutput.returnBookHeading();
        inputOutput.listCheckedOutBooks(books);
        String bookName = inputOutput.getUserInput();
        if (bookAbleToBeReturned(bookName))
        {
            for (Book book : books)
                if (book.getName().equals(bookName) && book.getCheckedOut())
                    book.returnThis();
            inputOutput.bookReturned();
        }
        else
        {
            inputOutput.bookNotAbleToBeReturned();
        }
    }

    public void checkOutBook()
    {
        if (loggedInUser == null)
        {
            inputOutput.youMustBeLoggedIn();
            return;
        }
        inputOutput.checkOutBookHeading();
        inputOutput.listBooks(books);
        String bookName = inputOutput.getUserInput();
        if (bookAvailable(bookName))
        {
            for (Book book : books)
                if (book.getName().equals(bookName) && !book.getCheckedOut())
                    book.checkOut(loggedInUser.getName());
            inputOutput.bookCheckedOut();
        }
        else
        {
            inputOutput.bookUnavailable();
        }
    }

    public void checkInMovie()
    {
        if (loggedInUser == null)
        {
            inputOutput.youMustBeLoggedIn();
            return;
        }
        inputOutput.returnMovieHeading();
        inputOutput.listCheckedOutMovies(movies);
        String movieName = inputOutput.getUserInput();
        if (movieAbleToBeReturned(movieName))
        {
            for (Movie movie : movies)
                if (movie.getName().equals(movieName) && movie.getCheckedOut())
                    movie.returnThis();
            inputOutput.movieReturned();
        }
        else
        {
            inputOutput.movieNotAbleToBeReturned();
        }
    }

    public void checkOutMovie()
    {
        if (loggedInUser == null)
        {
            inputOutput.youMustBeLoggedIn();
            return;
        }
        inputOutput.checkOutMovieHeading();
        inputOutput.listMovies(movies);
        String movieName = inputOutput.getUserInput();
        if (movieAvailable(movieName))
        {
            for (Movie movie : movies)
                if (movie.getName().equals(movieName) && !movie.getCheckedOut())
                    movie.checkOut(loggedInUser.getName());
            inputOutput.movieCheckedOut();
        }
        else
        {
            inputOutput.movieUnavailable();
        }
    }

    public boolean movieAvailable(String movieName)
    {
        for (Movie movie : movies)
            if (movie.getName().equals(movieName) && !movie.getCheckedOut())
                return true;
        return false;
    }

    public boolean movieAbleToBeReturned(String movieName)
    {
        for (Movie movie : movies)
            if (movie.getName().equals(movieName) && movie.getCheckedOut())
                return true;
        return false;
    }

    public boolean bookAvailable(String bookName)
    {
        for (Book book : books)
            if (book.getName().equals(bookName) && !book.getCheckedOut())
                return true;
        return false;
    }

    public boolean bookAbleToBeReturned(String bookName)
    {
        for (Book book : books)
            if (book.getName().equals(bookName) && book.getCheckedOut())
                return true;
        return false;
    }

    public void listBooks()
    {
        inputOutput.listBooks(books);
    }

    public void listMovies()
    {
        inputOutput.listMovies(movies);
    }

    public void showInfo()
    {
        if (loggedInUser == null)
        {
            inputOutput.youMustBeLoggedIn();
            return;
        }
        inputOutput.displayUserInfo(loggedInUser.getInfo());
    }

    public boolean getLoginStatus()
    {
        return loggedInUser != null;
    }
}
